package cn.findviewbydream.evil.app.websocket

import org.springframework.http.server.ServerHttpRequest
import org.springframework.http.server.ServerHttpResponse
import org.springframework.http.server.ServletServerHttpRequest
import org.springframework.web.socket.WebSocketHandler
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor
import javax.servlet.http.HttpSession

/********************************************
 * 工程名：MealOrder
 * 包名：cn.findviewbydream.evil.app.websocket
 * 时间：16-12-25
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：握手拦截器
 ********************************************/
class HandshakeInterceptor : HttpSessionHandshakeInterceptor()
{
    override fun beforeHandshake(request: ServerHttpRequest?, response: ServerHttpResponse?,
                                 wsHandler: WebSocketHandler?, attributes: MutableMap<String, Any>?): Boolean
    {
        if (request is ServletServerHttpRequest)
        {
            val session: HttpSession? = request.servletRequest.getSession(false)
            if (session != null)
            {
                val userId: String = session.getAttribute("WEBSOCKET_USERID") as String
                attributes?.put("WEBSOCKET_USERID", userId)
            }
        }
        return true
    }
}